package com.shuimin.as.service.excel;

import com.shuimin.as.model.repair.AsShopExtra;
import com.shuimin.as.model.repair.VAsRepairOrder;
import com.shuimin.table.XLSRow;
import com.shuimin.table.XLSTable;
import pond.common.S;
import pond.db.Record;
import pond.web.EndToEndException;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Created by sky on 2017/7/21.
 */
public class ImportService {

    public static List<VAsRepairOrder> importExcel(AsShopExtra extra,InputStream _is){
        //从excel中获取excel数据
        try (final InputStream is = _is) {
            XLSTable table = new XLSTable(is);
            XLSRow header=table.row(3);
            Map<String,Integer> columnIndexes=getColumnIndex(header,extra);
            return table.parse(4, table.rows(), (row) -> {
                VAsRepairOrder order=Record.newEntity(VAsRepairOrder.class);

                order.set("business_type",getData(row,columnIndexes.get("map_order_business_type")));
                order.set("name",getData(row,columnIndexes.get("map_customer_name")));
                order.set("send_name",getData(row,columnIndexes.get("map_customer_send_name")));
                order.set("license_plate",getData(row,columnIndexes.get("map_vehicle_license_plate")));
                order.set("send_phone",getData(row,columnIndexes.get("map_customer_send_phone")));
                order.set("vin",getData(row,columnIndexes.get("map_vehicle_vin")));
                order.set("brand",getData(row,columnIndexes.get("map_vehicle_brand")));
                order.set("model",getData(row,columnIndexes.get("map_vehicle_model")));
                order.set("version",getData(row,columnIndexes.get("map_vehicle_version")));
                order.set("mileage",getData(row,columnIndexes.get("map_vehicle_mileage")));
                order.set("is_appointment", "是".equals(String.valueOf(getData(row,columnIndexes.get("map_order_is_appointment")))) ? 1 : 0);
                Object sn=getData(row,columnIndexes.get("map_order_sn"));
                if(sn==null) return null;
                String _sn=String.valueOf(sn);
                if(_sn.trim().equals("")) return null;
                order.set("sn",getData(row,columnIndexes.get("map_order_sn")));
                order.set("repair_type",getData(row,columnIndexes.get("map_order_repair_type")));
                order.set("arrive_time", getData(row,columnIndexes.get("map_order_arrive_time")) instanceof Date ?
                                ((Date) getData(row,columnIndexes.get("map_order_arrive_time"))).getTime() : null);
                order.set("bill_time", getData(row,columnIndexes.get("map_order_bill_time")) instanceof Date ?
                        ((Date) getData(row,columnIndexes.get("map_order_bill_time"))).getTime() : null);
                order.set("expected_complete_time",getData(row,columnIndexes.get("map_order_expected_complete_time")) instanceof Date ?
                        ((Date) getData(row,columnIndexes.get("map_order_expected_complete_time"))).getTime() : null);
                order.set("completed_time",getData(row,columnIndexes.get("map_order_completed_time")) instanceof Date ?
                        ((Date) getData(row,columnIndexes.get("map_order_completed_time"))).getTime() : null);
                order.set("service_consultant",getData(row,columnIndexes.get("map_order_service_consultant")));
                order.set("insurance_company",getData(row,columnIndexes.get("map_order_insurance_company")));
                order.set("inspector",getData(row,columnIndexes.get("map_order_inspector")));
                order.set("receipt_sn",getData(row,columnIndexes.get("map_receipt_sn")));
                order.set("receipt_time",getData(row,columnIndexes.get("map_receipt_receipt_time")) instanceof Date ?
                        ((Date) getData(row,columnIndexes.get("map_receipt_receipt_time"))).getTime() : null);
                order.set("material_cost",getData(row,columnIndexes.get("map_receipt_material_cost")));
                order.set("man_hour_fee",getData(row,columnIndexes.get("map_receipt_man_hour_fee")));
                order.set("material_fee",getData(row,columnIndexes.get("map_receipt_material_fee")));
                order.set("total_fee",getData(row,columnIndexes.get("map_receipt_total_fee")));
                order.set("integral_deduction",getData(row,columnIndexes.get("map_receipt_integral_deduction")));
                order.set("longka_deduction",getData(row,columnIndexes.get("map_receipt_longka_deduction")));
                order.set("extra_deduction",getData(row,columnIndexes.get("map_receipt_extra_deduction")));
                order.set("deal_fee",getData(row,columnIndexes.get("map_receipt_deal_fee")));
                order.set("deposit_amount",getData(row,columnIndexes.get("map_receipt_deposit_amount")));
                order.set("remission_amount",getData(row,columnIndexes.get("map_receipt_remission_amount")));
                order.set("gross_profit",getData(row,columnIndexes.get("map_receipt_gross_profit")));
                return order;
            });

        } catch (IOException e) {
            throw new EndToEndException(400, "没有读取到XLS");
        }
    }

    private static Map<String,Integer> getColumnIndex(XLSRow row,AsShopExtra extra){
        //将extra的key和value反转
        Iterator iter=extra.keySet().iterator();
        Map<String,String> map=new HashMap<>();
        while (iter.hasNext()){
            String key= (String) iter.next();
            String value=extra.get(key);
            if(value!=null)
                map.put(value.trim(),key);
        }
        Map<String,Integer> ret=new HashMap<>();
        Integer size=row.size();
        for(int i=0;i<size;i++){
            Object _name=row.get(i);
            if(_name!=null){
                String name=String.valueOf(_name).trim();
                String value=map.get(name);
                if(value!=null) ret.put(value,i);
            }
        }
        return ret;
    }

    private static Object getData(XLSRow row,Integer index){
        if(index!=null) return row.get(index);
        else return null;
    }
}
